package com.zcah.spider;


import cn.edu.hfut.dmic.contentextractor.ContentExtractor;
import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler;
import com.zcah.dao.RedisUtil;
import com.zcah.model.Article;
import com.zcah.repository.ArticleService;
import com.zcah.util.URLParser;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WechatArticleSpider extends BreadthCrawler {
    private ArticleService articleService;
    private RedisUtil redisUtil;

   /* public WechatArticleSpider(String crawlPath, boolean autoParse) {
        super(crawlPath, autoParse);
    }*/

    public WechatArticleSpider( ArticleService articleService, RedisUtil redisUtil,
                               String crawlPath, boolean autoParse, List<String> url_list) throws Exception {
        super(crawlPath, autoParse);
        this.articleService = articleService;
        this.redisUtil = redisUtil;
        this.addSeed(url_list, true);
        this.setThreads(50);
        this.start(1);
    }

   /* public static void main(String[] args) throws Exception {
        String path = "https://mp.weixin.qq.com/s?__biz=MzIwOTE3Nzk5OA==&mid=403417832&idx=1&sn=6e4b6ec0ae4ec3ec807b88650ffd86de&scene=0#wechat_redirect";
        WechatArticleSpider spider = new WechatArticleSpider(path, true);
        spider.addSeed(path);
        spider.start(1);
    }*/

    public void visit(Page page, CrawlDatums crawlDatums) {
        Article article = new Article();

        String html = page.html();
        String url = page.url();

        article.setNickName(page.selectText("strong[class=profile_nickname]"));

        article.setWechatName(page.selectText("span[class=profile_meta_value]", 0));
        article.setFunction(page.selectText("span[class=profile_meta_value]", 1));
        article.setBiz(URLParser.fromURL(url).getParameter("__biz"));

        Pattern roundHeadImgPattern = Pattern.compile("round_head_img = \"(.*?)\"");
        Matcher m1 = roundHeadImgPattern.matcher(html);
        if (m1.find()) {
            String roundHeadImg = m1.group(0);
            int i = roundHeadImg.indexOf("\"");
            int j = roundHeadImg.lastIndexOf("\"");
            article.setRoundHeadImg(roundHeadImg.substring(i + 1, j));
        }

        //article
        article.setUrl(url);
        try {
            article.setContent(ContentExtractor.getContentByHtml(html));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Pattern descPattern = Pattern.compile("msg_desc = \"(.*?)\"");
        Matcher m2 = descPattern.matcher(html);
        if (m2.find()) {
            String descString = m2.group(0);
            int i = descString.indexOf("\"");
            int j = descString.lastIndexOf("\"");
            article.setDesc(descString.substring(i + 1, j));
        }

        Pattern msgCdnUrl = Pattern.compile("msg_cdn_url = \"(.*?)\"");
        Matcher m3 = msgCdnUrl.matcher(html);
        if (m3.find()) {
            String msgCdnUrlString = m3.group(0);
            int i = msgCdnUrlString.indexOf("\"");
            int j = msgCdnUrlString.lastIndexOf("\"");
            article.setMsgCdnUrl(msgCdnUrlString.substring(i + 1, j));
        }

        Pattern publishPattern = Pattern.compile("ct = \"(.*?)\"");
        Matcher m4 = publishPattern.matcher(html);
        if (m4.find()) {
            String publishString = m4.group(0);

            int i = publishString.indexOf("\"");
            int j = publishString.lastIndexOf("\"");
            long date = Long.parseLong(publishString.substring(i + 1, j));
            article.setPublishDate(new Date(date * 1000));
        }

        Pattern titlePattern = Pattern.compile("msg_title = \"(.*?)\"");
        Matcher m5 = titlePattern.matcher(html);
        if (m5.find()) {
            String titleString = m5.group(0);

            int i = titleString.indexOf("\"");
            int j = titleString.lastIndexOf("\"");
            article.setTitle(titleString.substring(i + 1, j));
        }
        article.setCrawlDate(new Date());
        article.setHtml(html);
        article.setWechatName(article.getWechatName());
        articleService.saveArticle(article);
    }
}